<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
  <div class="dashboard-container" id="category-app">
    <el-tree
      :data="data"
      show-checkbox
      node-key="id"
      default-expand-all
      check-on-click-node
      ref="tree"
      :props="defaultProps">
    </el-tree>
    <el-button type="primary" size="medium" @click="get()">
      确 定
    </el-button>
    <el-button type="primary" size="medium" @click="resetChecked">
      清 空
    </el-button>
  </div>

    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../js/index.js"></script>
    <script src="../../api/role.js"></script>
    <script>
      new Vue({
        el: '#category-app',
        data() {
          return {
            roleDto: {
              id: '',
              moduleIds: []
            },
            data: [{
              id: 1,
              label: '平台系统管理',
              children: [
                {
                id: 101,
                label: '权限管理',
                },
                {
                  id: 102,
                label: '模块管理',
                }
              ]
            }, {
              id: 2,
              label: '商品管理',
              children: [{
                id: 201,
                label: '商品资料'
              }, {
                id: 202,
                label: '采购订单'
              }, {
                id: 203,
                label: '销售管理'
              }, {
                id: 204,
                label: '库存预警'
              }, {
                id: 205,
                label: '客户订单'
              }]
            }, {
              id: 3,
              label: '会员管理',
              children: [{
                id: 301,
                label: '供应商管理'
              }, {
                id: 302,
                label: '客户管理'
              }, {
                id: 303,
                label: '部门人员'
              }]
            }, {
              id: 4,
              label: '分类管理',
              children: [{
                id: 401,
                label: '商品类别'
              }, {
                id: 402,
                label: '供应商类别'
              }]
            }],
            defaultProps: {
              children: 'children',
              label: 'label'
            }
          };
        },
        created(){
          this.roleDto.id = requestUrlParam('id')
          this.init()

        },
        mounted(){

        },
        methods: {
          async get(){
            var c = this.$refs.tree.getCheckedKeys()
            var d = c.concat(this.$refs.tree.getHalfCheckedKeys())
            this.roleDto.moduleIds = d
            let parms = { ...this.roleDto }
             setRoleModules(parms).then((res) => {
              if(String(res.code) === '1'){
                this.$message.success(res.data)
                // this.goBack()
              }else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          },
          setCheckedKeys() {
          },
          resetChecked() {
            this.$refs.tree.setCheckedKeys([]);
          },
          async init() {
            queryRoleById(this.roleDto.id).then((res) => {
              if (String(res.code) === '1') {
                this.$refs.tree.setCheckedKeys(res.data.moduleIds);
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          },
        },
        goBack() {
          window.parent.menuHandle(
                  {
                    id: '6',
                    url: '/backend/page/role/list.html',
                    name: '权限管理',
                  },
                  false
          )
        },

      })
      
    </script>

    <!-- <script>
      new Vue({
        el: '#category-app',
        data() {
          return {
            action: '',
            counts: 0,
            page: 1,
            pageSize: 10,
            tableData: [],
            classData: {
              'title': '添加角色分类',
              'dialogVisible': false,
              'name': '',
              'remark': ''
            }
          }
        },
        computed: {},
        created() {
          this.init()
        },
        mounted() {
        },
        methods: {
          async init () {
            await getRolePage({'page': this.page, 'pageSize': this.pageSize}).then(res => {
              if (String(res.code) === '1') {
                this.tableData = res.data.records
                this.counts = Number(res.data.total)
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            }).catch(err => {
              this.$message.error('请求出错了：' + err)
            })
          },
          handleQuery() {
            this.page = 1;
            this.init();
          },
           // 添加
          addClass(st) {
            this.classData.title = '新增角色分类'
            this.action = 'add'
            this.classData.name = ''
            this.classData.remark = ''
            this.classData.dialogVisible = true
          },
          editHandle(dat) {
            this.classData.title = '修改角色分类'
            this.action = 'edit'
            this.classData.name = dat.name
            this.classData.remark = dat.remark
            this.classData.id = dat.id
            this.classData.dialogVisible = true
          },
          // 关闭弹窗
          handleClose(st) {
            this.classData.dialogVisible = false
          },
          author(id){
            alert("Not Found");
          },
          //删除
          deleteHandle(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
              'type': 'warning'
            }).then(() => {
              deleRole(id).then(res => {
                if (res.code === 1) {
                  this.$message.success('删除成功！')
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            })
          },
          //数据提交
          submitForm(st) {
              const classData = this.classData
              const valid = (classData.name === 0 ||classData.name)  && (classData.remark === 0 || classData.remark)
              if (this.action === 'add') {
                if (valid) {
                  addRole({'name': classData.name, 'remark': classData.remark}).then(res => {
                    console.log(res)
                    if (res.code === 1) {
                      this.$message.success('分类添加成功！')
                      if (!st) {
                        this.classData.dialogVisible = false
                      } else {
                        this.classData.name = ''
                        this.classData.remark = ''
                      }
                      this.handleQuery()
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
              } else {
                  this.$message.error('请输入分类名称或备注')
              }
            } else if (valid) {
              editRole({'id':this.classData.id,'name': this.classData.name, 'remark': this.classData.remark}).then(res => {
                if (res.code === 1) {
                  this.$message.success('分类修改成功！')
                  this.classData.dialogVisible = false
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })

            } else {
              this.$message.error('请输入分类名称或备注')
            }
          },
          handleSizeChange (val) {
            this.pageSize = val
            this.init()
          },
          handleCurrentChange (val) {
            this.page = val
            this.init()
          }
        }
      })
    </script> -->
</body>
</html>